package com.yeraphael.controller;

import com.yeraphael.entity.Dept;
import com.yeraphael.entity.Result;
import com.yeraphael.service.DeptService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController     //等价于@Controller+@ResponseBody
public class DeptController {

    @Resource(name = "deptServiceImpl")
    private DeptService deptService;

    /**
     * 获取部门列表
     * @return
     */
    @GetMapping
    public Result list(){
        //1.调用service，获取数据
        List<Dept> depts = deptService.list();

        //3.响应数据（json格式）
        return Result.success(depts);
    }

    @DeleteMapping
    public Result delete(Integer id){
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Dept dept){
        System.out.println(dept);
        deptService.save(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getByid(@PathVariable Integer id){
        Dept dept = deptService.getByid(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }

}
